#pragma once
#include "Singleton.h"
#include <queue>
#include <mutex>
#include <thread>
#include <condition_variable>
#include <memory>
#include <unordered_map>
#include <functional>
#include <string>
#include "data.h"
#include <json/json.h>
#include <json/value.h>
#include <json/reader.h>


class LogicNode;
class CSession;

class LogicSystem:public Singleton<LogicSystem>
{
    using FunCallBack = std::function<void (std::shared_ptr<CSession>,const short& msg_id,const std::string &msg_data)>;
    friend class Singleton<LogicSystem>;
public:
    ~LogicSystem();

    void PostLogicNodeToQueue(std::shared_ptr<LogicNode> logic_node);


//私有方法
private:
    LogicSystem();
    void DealLogicNode();
    void Close();
    void RegisterFunCallBack();
    
    bool GetBaseInfo(const std::string & base_key,int uid,std::shared_ptr<UserInfo>& user_info);
    // 判断字符串是否全为数字
    bool isPureDigit(const std::string &str);
    
    void GetUserByUid(const std::string& uid_str,Json::Value& value);
    void GetUserByName(const std::string& name,Json::Value& value);


    // 回调函数
    void LoginHandler(std::shared_ptr<CSession> session,const short& msg_id,const std::string &msg_data);
    // 搜索用户
    void SearchUserHandler(std::shared_ptr<CSession> session,const short& msg_id,const std::string &msg_data);
    // 添加好友申请
    void AddFriendApplyHandler(std::shared_ptr<CSession> session,const short& msg_id,const std::string &msg_data);
    // 获取好友申请列表
    bool GetFriendApplyInfo(int to_uid,std::vector<std::shared_ptr<ApplyInfo>>& apply_list);

//私有属性
private:
    std::queue<std::shared_ptr<LogicNode>> _recv_msg_queue;
    std::mutex _recv_msg_queue_mutex;
    std::condition_variable _recv_msg_queue_cv;
    std::thread _recv_msg_thread;
    bool _stop_recv_msg_thread;

    std::unordered_map<int,FunCallBack> _fun_callbacks;

//    std::unordered_map<int, std::shared_ptr<UserInfo>> _users;
};
